/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.controller.bean;

import com.entites.Employee;
import com.model.bean.EmployeeFacadeLocal;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;
import javax.faces.view.facelets.FaceletContext;
import javax.inject.Named;
import javax.servlet.http.HttpSession;

/**
 *
 * @author VANANH
 */
@ManagedBean
@SessionScoped
public class EmployeeBean
{

    public final String ERROR_PASSWORD = "<span style=\"color:red\">Your password was incorrect.</span>";
    public final String SUCCESS_CHANGEPWD = "<span style=\"color:green\">Password Change Success!</span>";
    @EJB
    private EmployeeFacadeLocal employeeFacade;
    private String username;
    private String password;
    private String oldPassword;
    private String newPassword;
    private String email;
    private String gender;
    private String birthdate;
    private String possition;
    private String department;
    private String group;
    private String phone;
    private String mobilePhone;
    private String address;
    private String msg;

    public String getMsg()
    {
        return msg;
    }

    public void setMsg(String msg)
    {
        this.msg = msg;
    }

    FacesContext fCtx = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession) fCtx.getExternalContext().getSession(true);

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    public String getEmail()
    {
        email = getEmployee().getEmail();
        return email;
    }

    public void setEmail(String email)
    {
        this.email = email;
    }

    public String getGender()
    {
        gender = getEmployee().getGender();
        return gender;
    }

    public void setGender(String gender)
    {
        this.gender = gender;
    }

    public String getBirthdate()
    {
        SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy");
        birthdate = DATE_FORMAT.format(getEmployee().getBirthdate());
        return birthdate;
    }

    public void setBirthdate(String birthdate)
    {
        this.birthdate = birthdate;
    }

    public String getPossition()
    {
        possition = getEmployee().getPosition();
        return possition;
    }

    public void setPossition(String possition)
    {
        this.possition = possition;
    }

    public String getDepartment()
    {
        department = getEmployee().getDepartments();
        return department;
    }

    public void setDepartment(String department)
    {
        this.department = department;
    }

    public String getGroup()
    {
        group = getEmployee().getGroups();
        return group;
    }

    public void setGroup(String group)
    {
        this.group = group;
    }

    public String getPhone()
    {
        phone = getEmployee().getPhone();
        return phone;
    }

    public void setPhone(String phone)
    {
        this.phone = phone;
    }

    public String getMobilePhone()
    {
        mobilePhone = getEmployee().getMobilephone();
        return mobilePhone;
    }

    public void setMobilePhone(String mobilePhone)
    {
        this.mobilePhone = mobilePhone;
    }

    public String getAddress()
    {
        address = getEmployee().getAddress();
        return address;
    }

    public void setAddress(String address)
    {
        this.address = address;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public String getOldPassword()
    {
        oldPassword = getEmployee().getPassword();
        return oldPassword;
    }

    public void setOldPassword(String oldPassword)
    {
        this.oldPassword = oldPassword;
    }

    public String getNewPassword()
    {
        return newPassword;
    }

    public void setNewPassword(String newPassword)
    {
        this.newPassword = newPassword;
    }

    /**
     * Creates a new instance of EmployeeBean
     */
    public EmployeeBean()
    {
        msg = null;
    }

    private Employee getEmployee()
    {
        username = (String) session.getAttribute("username");
        return employeeFacade.find(username);
    }

    public String saveChangeProfile()
    {
        try
        {
            Employee e = getEmployee();
            e.setPhone(phone);
            e.setMobilephone(mobilePhone);
            e.setAddress(address);
            employeeFacade.edit(e);
            msg = "Save Change Success!";
        } catch (Exception e)
        {
            msg = e.getMessage();
        }
        return msg;
    }

    public String changePassword()
    {
        msg = "";
        oldPassword = getEmployee().getPassword();
        if (!password.equals(oldPassword))
        {
            msg = ERROR_PASSWORD;
        } else
        {
            Employee e = getEmployee();
            e.setPassword(newPassword);
            employeeFacade.edit(e);
            msg = SUCCESS_CHANGEPWD;
        }
        return msg;
    }

    public void cancel()
    {
        this.msg = null;
        FacesContext.getCurrentInstance().getMessages().remove();
    }

    public void validatePassword(ComponentSystemEvent event)
    {

        FacesContext fc = FacesContext.getCurrentInstance();

        UIComponent components = event.getComponent();

        // get password
        UIInput uiInputPassword = (UIInput) components.findComponent("txtConfirmPassword");
        String password = uiInputPassword.getLocalValue() == null ? ""
                : uiInputPassword.getLocalValue().toString();
        String passwordId = uiInputPassword.getClientId();

        // get confirm password
        UIInput uiInputConfirmPassword = (UIInput) components.findComponent("txtNewPassword");
        String confirmPassword = uiInputConfirmPassword.getLocalValue() == null ? ""
                : uiInputConfirmPassword.getLocalValue().toString();

        // Let required="true" do its job.
        if (password.isEmpty() || confirmPassword.isEmpty())
        {
            return;
        }

        if (!password.equals(confirmPassword))
        {

            FacesMessage msg = new FacesMessage(" * Password must match!");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            fc.addMessage(passwordId, msg);
            fc.renderResponse();

        }
    }
}
